<?php

class JWPBlog {
	// a class to fetch wordpress objects, custom wordpress objects, etc.
	private static $_wpdb;

	private static $_Instance;

	private function __construct(){
		self::setConstant('ABSPATH',JPATH_COMPONENT_ADMINISTRATOR.DS.'wordpress_mod'.DS);
		self::instantiateWP();		
		return $this;
	}

	public static function getInstance(){
		if(!self::$_Instance){
			self::$_Instance = new JWPBlog();
		}
		return self::$_Instance;
	}

	public function setConstant($varname, $value){
		define(ucwords($varname),$value);
	}
	public static function instantiateWP(){
		self::loadCoreClasses();
		//orig file: wp-config.php
		define('DB_NAME', 'joomla');    // The name of the database
		define('DB_USER', 'joomla');     // Your MySQL username
		define('DB_PASSWORD', 'bpkrak603'); // ...and password
		define('DB_HOST', 'localhost');    // 99% chance you won't need to change this value
		define('DB_CHARSET', 'utf8');
		define('DB_COLLATE', '');

		// Change each KEY to a different unique phrase.  You won't have to remember the phrases later,
		// so make them long and complicated.  You can visit http://api.wordpress.org/secret-key/1.1/
		// to get keys generated for you, or just make something up.  Each key should have a different phrase.
		define('AUTH_KEY', 'auth key'); // Change this to a unique phrase.
		define('SECURE_AUTH_KEY', 'secure auth key'); // Change this to a unique phrase.
		define('LOGGED_IN_KEY', 'logged in key'); // Change this to a unique phrase.

		// You can have multiple installations in one database if you give each a unique prefix
		//$table_prefix  = 'jwp_';   // Only numbers, letters, and underscores please!
		define('WP_TABLE_PREFIX','jos_wp_');
		// Change this to localize WordPress.  A corresponding MO file for the
		// chosen language must be installed to wp-content/languages.
		// For example, install de.mo to wp-content/languages and set WPLANG to 'de'
		// to enable German language support.
		define ('WPLANG', '');
		
		//TODO: possible mod this to fit Joomla!'s needs
		if ( !defined('WP_MEMORY_LIMIT') )
		define('WP_MEMORY_LIMIT', '32M');
		if ( function_exists('memory_get_usage') && ( (int) @ini_get('memory_limit') < abs(intval(WP_MEMORY_LIMIT)) ) )
		@ini_set('memory_limit', WP_MEMORY_LIMIT);
		
		//replaces global $blog_id
		define('BLOG_ID',1);
		
		//is will be important, if not critical for safest practices
		if ( isset($_REQUEST['GLOBALS']) or !empty($_GLOBALS) ){
			die('GLOBALS overwrite attempt detected');
		}
		$PHP_SELF = $_SERVER['PHP_SELF'];
		if ( empty($PHP_SELF) ){
			$_SERVER['PHP_SELF'] = $PHP_SELF = preg_replace("/(\?.*)?$/",'',$_SERVER["REQUEST_URI"]);
		}
		
		

	}
	
	public static function instantiateWPAdmin(){
		$jwpblog = self::getInstance();		
		$jwpblog->loadAdminClasses();

	}
	public static function loadAdminClasses(){
		//special classes for admin
	}

	public static function loadCoreClasses(){
		$classes = scandir(JWPBLOG_PATH_ADMIN.DS.'classes');
		foreach($classes as $file){//skip .,..,.svn
			if(strpos($file,'.') !== 0){
				if(file_exists(JWPBLOG_PATH_ADMIN.DS.'classes'.DS.$file)){
					require_once(JWPBLOG_PATH_ADMIN.DS.'classes'.DS.$file);
				}else{
					echo $file.' not found. Functionality will be limited';
				}
			}
		}
	}

	public static function getWpdb(){
		if(!self::$_wpdb){
			self::$_wpdb =& new wpdb();
		}
		return self::$_wpdb;
	}

	//TODO: merge with wp user function?
	public static function getCurrentUser(){
		if(!self::$_current_user){
			self::$_current_user =& JFactory::getUser();
		}
		return self::$_current_user;
	}

	//TODO: find and refactor what is setting comment value
	public static function getComment(){
		global $comment;
		if(!self::$_comment){
			self::$_comment =& $comment;
		}
		return self::$_comment;
	}

	//TODO: find and refactor global
	public static function getFileDesc(){
		global $wp_file_descriptions;
		if(!self::$_file_desc){
			self::$_file_desc =& $wp_file_descriptions;
		}
		return self::$_file_desc;
	}

	public static function getFileSys(){
		global $wp_filesystem;
		if(!self::$_file_sys){
			self::$_file_sys =& $wp_filesystem;
		}
		return self::$_file_sys;
	}
}
?>